package cn.iocoder.yudao.module.wms.controller.admin.distribution;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.distribution.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.distribution.DistributionDO;
import cn.iocoder.yudao.module.wms.service.distribution.DistributionService;

@Tag(name = "管理后台 - 配送")
@RestController
@RequestMapping("/wms/distribution")
@Validated
public class DistributionController {

    @Resource
    private DistributionService distributionService;

    @PostMapping("/create")
    @Operation(summary = "创建/申请配送")
    @PreAuthorize("@ss.hasPermission('wms:distribution:create')")
    public CommonResult<String> createDistribution(@Valid @RequestBody DistributionSaveReqVO reqVO) {
        return success(distributionService.createDistribution(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送")
    @PreAuthorize("@ss.hasPermission('wms:distribution:update')")
    public CommonResult<Boolean> updateDistribution(@Valid @RequestBody DistributionSaveReqVO reqVO) {
        distributionService.updateDistribution(reqVO);
        return success(true);
    }

    /*@DeleteMapping("/delete")
    @Operation(summary = "删除配送")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:distribution:delete')")
    public CommonResult<Boolean> deleteDistribution(@RequestParam("id") String id) {
        distributionService.deleteDistribution(id);
        return success(true);
    }*/

    @GetMapping("/get")
    @Operation(summary = "获得配送")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:distribution:query')")
    public CommonResult<DistributionRespVO> getDistribution(@RequestParam("id") String id) {
        DistributionDO distribution = distributionService.getDistribution(id);
        return success(BeanUtils.toBean(distribution, DistributionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送分页")
    @PreAuthorize("@ss.hasPermission('wms:distribution:query')")
    public CommonResult<PageResult<DistributionRespVO>> getDistributionPage(@Valid DistributionPageReqVO reqVO) {
        return success(distributionService.getDistributionPage2(reqVO));
    }

    /*@GetMapping("/export-excel")
    @Operation(summary = "导出配送 Excel")
    @PreAuthorize("@ss.hasPermission('wms:distribution:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDistributionExcel(@Valid DistributionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributionDO> list = distributionService.getDistributionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送.xls", "数据", DistributionRespVO.class,
                        BeanUtils.toBean(list, DistributionRespVO.class));
    }*/

    @GetMapping("/list")
    @Operation(summary = "获得配送列表（无分页）")
    @PreAuthorize("@ss.hasPermission('wms:distribution:query')")
    public CommonResult<List<DistributionRespVO>> getDistributionList() {
        // 大数据量慎用
        List<DistributionDO> list = distributionService.list();
        return success(BeanUtils.toBean(list, DistributionRespVO.class));
    }

    @PutMapping("/confirm-audit/{id}")
    @Operation(summary = "配送审核确认")
    @PreAuthorize("@ss.hasPermission('wms:distribution:update')")
    public CommonResult<Boolean> confirmAudit(@PathVariable String id) {
        distributionService.confirmAudit(id);
        return success(true);
    }

    @PutMapping("/confirm-ok/{id}")
    @Operation(summary = "配送完成确认")
    @PreAuthorize("@ss.hasPermission('wms:distribution:update')")
    public CommonResult<Boolean> confirmOk(@PathVariable String id) {
        distributionService.confirmOk(id);
        return success(true);
    }

}